package com.cskaoyan.wordmemorize.controller.admin;


import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserVocAISituationCommand;
import com.cskaoyan.wordmemorize.service.AIReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/admin/voc/ai-read")
public class AIReadController {
    @Autowired
    AIReadService aiReadService;


    // http://113.46.193.206:8111/admin/voc/ai-read/situation
    // 添加ai阅读模式
    // 问题:接收的是http请求体里的json字符串，json字符串怎么转化成了command的一个对象？
    // springboot里添加的依赖会自动帮我们转换成command对象
    @SaAdminCheckPermission("admin:ai-read:situation:add")
    @PostMapping("/situation")
    public Result addAISituation(@RequestBody UserVocAISituationCommand command) {
        aiReadService.addAISituation(command);
        return Result.ok();
    }


    //修改指定ai阅读的模式
    // http://113.46.193.206:8111/admin/voc/ai-read/situation
    @SaAdminCheckPermission("admin:ai-read:situation:update")
    @PutMapping("/situation")
    public Result updateAISituation(@RequestBody UserVocAISituationCommand command) {
        aiReadService.updateAISituation(command);
        return Result.ok();
    }

    // 批量删除ai阅读模式
    // http://113.46.193.206:8111/admin/voc/ai-read/situation
    @SaAdminCheckPermission("admin:ai-read:situation:batchdelete")
    @DeleteMapping("/situation")
    public Result batchDeleteAISituationOption(@RequestBody BatchDeleteCommand batchDeleteCommand) {
        List<Long> ids = new ArrayList<>();
        for (String id : batchDeleteCommand.getIds()){
            ids.add(Long.parseLong(id));
        }
        aiReadService.batchDeleteAISituationOption(ids);
        return Result.ok();
    }


    // 删除指定的ai阅读模式
    // http://113.46.193.206:8111/admin/voc/ai-read/situation/1946107570771300354
    @SaAdminCheckPermission("admin:ai-read:situation:delete")
    @DeleteMapping ("/situation/{id}")
    public Result deleteAISituation(@PathVariable("id") Long id) {
        aiReadService.deleteAISituation(id);
        return Result.ok();
    }

    // 获取ai阅读的场景列表
    //http://113.46.193.206:8111/admin/voc/ai-read/situation/list?pageNum=1&count=10&keyword=test
    @SaAdminCheckPermission("admin:ai-read:situation:search")
    @GetMapping("/situation/list")
    public Result<PageDTO<UserVocAISituationDTO>>  getAISituationList(PageRequest pageRequest) {
        PageDTO<UserVocAISituationDTO> aiSituationList = aiReadService.getAISituationList(pageRequest);
        return Result.ok(aiSituationList);
    }

    // 删除指定的ai阅读模式的选项
    // http://113.46.193.206:8111/admin/voc/ai-read/option/1946120271677370369
    @DeleteMapping("/option/{id}")
    public Result deleteAISituationOption(@PathVariable("id") Long id){
        aiReadService.deleteAISituationOption(id);
        return Result.ok();
    }


}
